//
//  ZHTools.m
//  ZHS_UCDemo
//
//  Created by Emrys on 2016/11/8.
//  Copyright © 2016年 Emrys. All rights reserved.
//

#import "ZHTools.h"
#import <CommonCrypto/CommonCrypto.h>

#define FileHashDefaultChunkSizeForReadingData 1024*8 // 8K

static const NSString *kZHDemo = @"Demo";
static const NSString *kZHPartyAName = @"PartyAName";
static const NSString *kZHPartyBName = @"PartyBName";
static const NSString *kZHAppTitle = @"AppTitle";
static const NSString *kZHLeader = @"Leader";

@implementation ZHTools

#pragma mark -
#pragma mark - isDemo
+ (BOOL)storeUpIsDemo:(BOOL)isDemo{
    [[NSUserDefaults standardUserDefaults] setBool:isDemo forKey:(NSString *)kZHDemo];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (BOOL)isDemo {
    return [[NSUserDefaults standardUserDefaults] boolForKey:(NSString *)kZHDemo];
}

#pragma mark - partyAName
+ (BOOL)storeUpPartyAName:(NSString *)partyAName {
    [[NSUserDefaults standardUserDefaults] setObject:partyAName forKey:(NSString *)kZHPartyAName];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSString *)partyAName {
    return [[NSUserDefaults standardUserDefaults] objectForKey:(NSString *)kZHPartyAName];
}

#pragma mark - partyBName
+ (BOOL)storeUpPartyBName:(NSString *)partyBName {
    [[NSUserDefaults standardUserDefaults] setObject:partyBName forKey:(NSString *)kZHPartyBName];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSString *)partyBName {
    return [[NSUserDefaults standardUserDefaults] objectForKey:(NSString *)kZHPartyBName];
}

#pragma mark - appTitle
+ (BOOL)storeUpAppTitle:(NSString *)appTitle {
    [[NSUserDefaults standardUserDefaults] setObject:appTitle forKey:(NSString *)kZHAppTitle];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSString *)appTitle {
    return [[NSUserDefaults standardUserDefaults] objectForKey:(NSString *)kZHAppTitle];
}

#pragma mark - leaderSymbol
+ (BOOL)storeUpLeaderWithLeaderSymbol:(NSInteger)leaderSymbol {
    [[NSUserDefaults standardUserDefaults] setInteger:leaderSymbol forKey:(NSString *)kZHLeader];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSInteger)leaderSymbol {
    return [[NSUserDefaults standardUserDefaults] integerForKey:(NSString *)kZHLeader];
}

#pragma mark -
#pragma mark - MD5
+ (NSString *)MD5WithString:(NSString *)string {

    const char *cStr = [string UTF8String];
    unsigned char digest[16];
     unsigned int x=(int)strlen(cStr) ;
    CC_MD5( cStr, x, digest );
    // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;

}
+ (NSString *)MD5WithData:(NSData *)data {
    
    const char* original_str = (const char *)[data bytes];
    unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16
    CC_MD5(original_str, (uint)strlen(original_str), digist);
    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];
    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){
        [outPutStr appendFormat:@"%02x",digist[i]];//小写x表示输出的是小写MD5，大写X表示输出的是大写MD5
    }
    
    return [outPutStr lowercaseString];
}
+ (NSString *)MD5WithFilePath:(NSString *)path {
    return (__bridge_transfer NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path,FileHashDefaultChunkSizeForReadingData);
}
CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath,
                                      size_t chunkSizeForReadingData) {
    
    // Declare needed variables
    CFStringRef result = NULL;
    CFReadStreamRef readStream = NULL;
    
    // Get the file URL
    CFURLRef fileURL =
    CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                  (CFStringRef)filePath,
                                  kCFURLPOSIXPathStyle,
                                  (Boolean)false);
    
    CC_MD5_CTX hashObject;
    bool hasMoreData = true;
    bool didSucceed;
    
    if (!fileURL) goto done;
    
    // Create and open the read stream
    readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
                                            (CFURLRef)fileURL);
    if (!readStream) goto done;
    didSucceed = (bool)CFReadStreamOpen(readStream);
    if (!didSucceed) goto done;
    
    // Initialize the hash object
    CC_MD5_Init(&hashObject);
    
    // Make sure chunkSizeForReadingData is valid
    if (!chunkSizeForReadingData) {
        chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
    }
    
    // Feed the data to the hash object
    while (hasMoreData) {
        uint8_t buffer[chunkSizeForReadingData];
        CFIndex readBytesCount = CFReadStreamRead(readStream,
                                                  (UInt8 *)buffer,
                                                  (CFIndex)sizeof(buffer));
        if (readBytesCount == -1)break;
        if (readBytesCount == 0) {
            hasMoreData =false;
            continue;
        }
        CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount);
    }
    
    // Check if the read operation succeeded
    didSucceed = !hasMoreData;
    
    // Compute the hash digest
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &hashObject);
    
    // Abort if the read operation failed
    if (!didSucceed) goto done;
    
    // Compute the string result
    char hash[2 *sizeof(digest) + 1];
    for (size_t i =0; i < sizeof(digest); ++i) {
        snprintf(hash + (2 * i),3, "%02x", (int)(digest[i]));
    }
    result = CFStringCreateWithCString(kCFAllocatorDefault,
                                       (const char *)hash,
                                       kCFStringEncodingUTF8);
    
done:
    
    if (readStream) {
        CFReadStreamClose(readStream);
        CFRelease(readStream);
    }
    if (fileURL) {
        CFRelease(fileURL);
    }
    return result;
}

#pragma mark -
#pragma mark - readFile
+ (NSDictionary *)dictionaryWithFile:(NSString *)filePath {
    return [[NSDictionary alloc] initWithContentsOfFile:filePath];
}
@end
